package top.z.work.module.system.controller.dict;

import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import top.z.work.framework.common.pojo.PageInfo;
import top.z.work.framework.common.pojo.R;
import top.z.work.module.system.controller.dict.vo.SystemDictDataCreateVO;
import top.z.work.module.system.controller.dict.vo.SystemDictDataModifyVO;
import top.z.work.module.system.controller.dict.vo.SystemDictDataVO;
import top.z.work.module.system.convert.SystemDictDataConvert;
import top.z.work.module.system.entity.SystemDictData;
import top.z.work.module.system.service.SystemDictDataService;

import java.util.List;

/**
 * 字典数据管理Controller
 *
 * @author zaoangod
 * @since 2024-05-10 16:38:44
 */
@Tag(name = "字典数据管理")
@Slf4j
@Validated
@RestController
@RequestMapping("system/dict-data")
public class SystemDictDataController {

    @Resource
    private SystemDictDataService systemDictDataService;

    @Operation(summary = "字典数据分页", description = "获取字典数据分页")
    @GetMapping("page")
    public R<PageInfo<SystemDictDataVO>> list(
            SystemDictDataVO parameter,
            @RequestParam(name = "page", defaultValue = "1") Integer page,
            @RequestParam(name = "size", defaultValue = "10") Integer size
    ) {
        QueryWrapper         query    = SystemDictDataConvert.INSTANCE.convert(parameter);
        Page<SystemDictData> data     = Page.of(page, size);
        Page<SystemDictData> pageInfo = systemDictDataService.page(data, query);
        return R.s(SystemDictDataConvert.INSTANCE.convert(pageInfo));
    }

    @Operation(summary = "字典数据列表", description = "获取字典数据列表")
    @GetMapping("list")
    public R<List<SystemDictDataVO>> list(SystemDictDataVO parameter) {
        QueryWrapper         query = SystemDictDataConvert.INSTANCE.convert(parameter);
        List<SystemDictData> list  = systemDictDataService.list(query);
        return R.s(SystemDictDataConvert.INSTANCE.convert(list));
    }

    @Operation(summary = "字典数据信息", description = "获取字典数据信息")
    @GetMapping("info")
    public R<SystemDictDataVO> queryOne(SystemDictDataVO parameter) {
        QueryWrapper   query          = SystemDictDataConvert.INSTANCE.convert(parameter);
        SystemDictData SystemDictData = systemDictDataService.getOne(query);
        return R.s(SystemDictDataConvert.INSTANCE.convert(SystemDictData));
    }

    @Operation(summary = "删除字典数据", description = "根据字典数据ID删除数据")
    @DeleteMapping("remove")
    public R<Boolean> remove(@RequestParam("identity") @NotNull(message = "数据主键不能为空") Long identity) {
        systemDictDataService.removeById(identity);
        return R.s(true);
    }

    @Operation(summary = "修改字典数据", description = "根据字典数据ID修改数据")
    @PutMapping("modify")
    public R<Boolean> modify(@RequestBody SystemDictDataModifyVO parameter) {
        SystemDictData dept    = SystemDictDataConvert.INSTANCE.convert(parameter);
        boolean        success = systemDictDataService.updateById(dept);
        return success ? R.s(true) : R.f();
    }

    @Operation(summary = "添加字典数据", description = "添加字典数据数据")
    @PostMapping("add")
    public R<Boolean> add(@RequestBody SystemDictDataCreateVO parameter) {
        SystemDictData dept    = SystemDictDataConvert.INSTANCE.convert(parameter);
        boolean        success = systemDictDataService.save(dept);
        return success ? R.s(true) : R.f();
    }

    @Operation(summary = "批量删除", description = "批量删除字典数据数据")
    @PostMapping("batch-remove")
    public R<Boolean> batchRemove(
            @RequestBody
            @NotNull(message = "数据主键列表不能为空")
            @NotEmpty(message = "数据主键列表不能为空")
            List<String> identityList
    ) {
        boolean success = systemDictDataService.removeByIds(identityList);
        return success ? R.s(true) : R.f();
    }

}
